<?php
class Vlogs {
	var $collection		= null;		
	var $cache			= null;
	var $clear			= null;
	protected $_operators = array(
									'<'   => '$lt',
									'>'   => '$gt',
									'<='  =>  '$lte',
									'>='  => '$gte',
									'!='  => array('single' => '$ne', 'multiple' => '$nin'),
									'<>'  => array('single' => '$ne', 'multiple' => '$nin'),
									'or'  => '$or',
									'||'  => '$or',
									'not' => '$not',
									'!'   =>  '$not'
								);
	/*
	------- table vlogs
	------- field
	* userid
	* fullhouseid
	* type: {
				1: convert from tlike
				2: tranfer private to public
				3: card mobile
				4: payment
				5: sms
				6: buy
				7: sole
				8: give gift
				9: give von
				10: create item
				11: admin give
				12: intro
			}
	* content: json {von;....}
	* time
	*/
	public function __construct() {
		if(DB_MONGO_ENABLE) {
			$mongo 				= new Mongo();
			$this->collection 	= $mongo->selectDB('vfriend')->selectCollection('vlogs');				
		}
	}
	public function delete() {
		if(DB_MONGO_ENABLE) {
			$this->collection->remove();
		}
	}
	public function deleteByTime($intUserId) {
		if(DB_MONGO_ENABLE) {
			$intTime = time() - LOGS_DELETE_TIME; 
			$this->collection->remove(array('time'=>'$lt ' . $intTime));
		}
	}
	public function deleteById($intId) {
		if(DB_MONGO_ENABLE) {
			$this->collection->remove(array('_id' => new MongoId($intId)), true);
		}
	}
	public function deleteByType($intType, $intSrcId) {
		if(DB_MONGO_ENABLE) {
			$this->collection->remove(array('type'=>$intType,'srcid'=>(int)$intSrcId));
		}
	}
	public function insert($intUserId, $intFullhouseId, $intType, $strContent) {	
		try {
			if(DB_MONGO_ENABLE) {
				$objInsert = array(	'userid'		=> (int)$intUserId, 
									'fullhouseid'	=> (int)$intFullhouseId,
									'type' 			=> (int)$intType,
									'content'		=> (string)$strContent,
									'time'			=> time(),
							);		
				$this->collection->insert($objInsert);				
			}
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vlogs::insert - ' .$e->getMessage());			
			return null;
		}
	}
	public function getVlogsByUserId($intUserId, $intOffset, $intLimit, &$intTotal=0) {
		try {			
			if(DB_MONGO_ENABLE) {				
				$objFind 	= array('userid'=>(int)$intUserId);
				$objSort	= array('time'=>-1);
				$arrData 	= $this->collection->find($objFind)->sort($objSort)->limit((int)$intLimit)->skip((int)$intOffset);
				$intTotal	= $this->collection->find($objFind)->count();				
				if((object)$arrData) {
					$arrResult = null;
					foreach($arrData as $value) {
						$value['postdate'] 	= date('d/m/Y H:i:s',$value['time']);
						$value['content']	= Zend_Json::decode($value['content']);
						$arrResult[] 		= $value;
					}
					return $arrResult;
				}
			}
			return null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vlogs::getVlogsByUserId - ' .$e->getMessage());			
			return null;
		}
	}
}
?>